<template>
  <section class="root-container">
    <van-sticky :offset-top="50">
      <van-search
        v-model="value"
        placeholder="请输入搜索关键词"
        background="#4fc08d"
      />
    </van-sticky>
    <template v-for="item in sheetList" :key="item.id">
      <van-cell :title="item.title" is-link @click="checkInventory(item.to)" />
    </template>
    <van-cell />
  </section>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { getStorage } from '@/utils/storage'
import { Dialog } from 'vant'
import { Evaluation } from '@/api/Evaluation'

const EvaluationRequest = new Evaluation()

const value = ref('')
const router = useRouter()

const checkInventory = (to) => {
  if (!getStorage('uid')) {
    Dialog.alert({
      title: '您未登录',
      message: '请先登录',
    }).then(() => {
      router.replace({ name: 'Login' })
    })

    return
  }

  EvaluationRequest.getTodayEvaluation({ title: to.name }).then((result) => {
    console.log(result.data.result)
    if (result.data.result) {
      Dialog.alert({
        title: '提示',
        message: '你今天已经测过这张问卷了',
      })
    } else {
      router.push(to)
    }
  })
}

const sheetList = ref([
  {
    id: 0,
    title: '每日SDS量表',
    name: 'SDS',
    to: {
      name: 'SDS',
    },
  },
  {
    id: 1,
    title: '每日HCL-32量表',
    name: 'HCL',
    to: {
      name: 'HCL',
    },
  },
])
</script>

<style scoped>
.root-container {
  width: 100vw;
  height: calc(100vh - 100px);
  background: #efefef;
}
</style>
